package com.runservice;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.socket.SocketService;
import com.utils.Base64;
import com.utils.SecureUtil;

public class RunService {
	/** 版本号-1.0 */
	public static final String version_1_0 = "1.0";
	/** 版本号-2.0 */
	public static final String version_2_0 = "2.1";
	/** 企业编号 */
	public static final String enterpriseNo = "10001";
	/** 机构编号 */
	public static final String merId = "777290058150379";
	/** 清算标识-T0 */
	public static final String settType_0 = "0";
	/** 清算标识-T1 */
	public static final String settType_1 = "1";
	/** 交易类型-交易请求 */
	public static final String txnType_12 = "12";
	/** 交易类型-交易查询 */
	public static final String txnType_00 = "00";
	/** 交易类型-余额查询 */
	public static final String txnType_01 = "01";
	/** 签名方法固定填写 */
	public static final String signMethod = "01";
	/** 交易地址 */
	public static String ServerIP = "123.206.54.253";
	/** 交易端口 */
	public static int ServerPort = 8830;

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map = payService();
		// map=payQuery();
		// map=balanceQuery();
		Map<String, String> reqMap = SecureUtil.submitDate(SecureUtil.signData(map));
		String resp = new SocketService().connServer(reqMap, ServerIP,ServerPort);
		Map<String, String> respMap = JSONObject.fromObject(resp);
		System.out.println("异步应答报文:"+getAsnresp(reqMap));
		try {
			boolean bool = SecureUtil.checkCert(SecureUtil.signData(respMap).toString().getBytes("UTF-8"),Base64.getFromBase64(respMap.get("signature")).getBytes("UTF-8"));
			System.out.println("验签结果:" + bool);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 ********************************************************* .<br>
	 * [方法] payService <br>
	 * [描述] (交易请求) <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] String <br>
	 * [时间] 2017-8-7 上午11:45:28 <br>
	 ********************************************************* .<br>
	 */
	public static Map<String, String> payService() {
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put("version", version_2_0);
		reqMap.put("orderId",
				new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		reqMap.put("enterpriseNo", enterpriseNo);
		reqMap.put("merId", "777290058150379");
		reqMap.put("txnType", txnType_12);
		reqMap.put("txnTime",
				new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		reqMap.put("payName", "全渠道");
		reqMap.put("accNo", "6216261000000000018");
		reqMap.put("txnAmt", "100");
		reqMap.put("BankName", "平安银行");
		reqMap.put("signMethod", signMethod);
		reqMap.put("settType", settType_1);
		reqMap.put("ppType", "0");
		reqMap.put("note", "test");
		reqMap.put("backUrl", "47.94.139.197:1755");
		// 2.0版本选填
		reqMap.put("certifTp", "A1");
		/* reqMap.put("certifId", "652701197606021164"); */
		reqMap.put("certifId", "218366");
		reqMap.put("sourcesOfFunds", "00");
		
		return reqMap;
	}
	
	/**
	 * 获取异步报文
	 */
	public static String getAsnresp(Map<String, String> reqMap){
		String infoString="";
		if (reqMap.get("backUrl") != null) {
			ServerSocket server = null;
			String[] ipPort = reqMap.get("backUrl").split(":");
			//String ip = ipPort[0];
			int port = Integer.valueOf(ipPort[1]);
			try {
				server = new ServerSocket(port);
				Socket socket = null;
				socket = server.accept();
				String line="";
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				infoString=in.readLine();
				//System.out.println("异步应答报文:" + infoString);
				in.close(); 		// 关闭Socket输入流
				socket.close(); 	// 关闭Socket
				server.close(); 	// 关闭ServerSocket
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println("Error." + e);
			}
		}
		return infoString;
	}

	/**
	 * 
	 ********************************************************* .<br>
	 * [方法] payQuery <br>
	 * [描述] (交易查询) <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] void <br>
	 * [时间] 2017-8-7 上午11:46:07 <br>
	 ********************************************************* .<br>
	 */
	public static Map<String, String> payQuery() {
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put("version", version_2_0);
		reqMap.put("enterpriseNo", enterpriseNo);
		reqMap.put("merId", merId);
		reqMap.put("signMethod", signMethod);
		reqMap.put("orderId", "20171103154824");
		reqMap.put("txnType", txnType_00);
		reqMap.put("txnTime", "20171103154824");
		return reqMap;
	}

	/**
	 * 
	 ********************************************************* .<br>
	 * [方法] balanceQuery <br>
	 * [描述] (余额查询) <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] Map<String,String> <br>
	 * [时间] 2017-8-7 下午12:17:31 <br>
	 ********************************************************* .<br>
	 */
	public static Map<String, String> balanceQuery() {
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put("version", version_2_0);
		reqMap.put("enterpriseNo", enterpriseNo);
		reqMap.put("merId", merId);
		reqMap.put("signMethod", signMethod);
		reqMap.put("txnType", txnType_01);
		reqMap.put("settType", settType_1);
		return reqMap;
	}
}
